package com.nkzy.controller;

import com.nkzy.Pojo.user;
import com.nkzy.mapper.usermapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class userController {

    @Autowired
    private usermapper userMapper;

    public Response<String> selectAllUser() {
        return null;
    }

    public Response<user> selectUserByUsername(String test) {
        return null;
    }

    // 定义通用返回结果类
    public static class Response<T> {
        private int code;
        private String message;
        private T data;

        public Response() {
            this.code = code;
            this.message = message;
            this.data = data;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        public void setSuccess(boolean b) {
        }

        public boolean isSuccess() {
            return false;
        }
    }

    // 插入用户
    @PostMapping("/insert")
    public Response<String> insertUser(@RequestBody user user) {
        int result = userMapper.insertUser(user);
        if (result > 0) {
            return new Response<>();
        } else {
            return new Response<>();
        }
    }

    // 根据ID查询用户
    @GetMapping("/select/{id}")
    public Response<user> selectUserById(@PathVariable Integer id) {
        user user = userMapper.selectUserById(id);
        if (user != null) {
            return new Response<>();
        } else {
            return new Response<>();
        }
    }

    // 更新用户信息
    @PutMapping("/update")
    public Response<String> updateUser(@RequestBody user user) {
        int result = userMapper.updateUser(user);
        if (result > 0) {
            return new Response<>();
        } else {
            return new Response<>();
        }
    }

    // 根据ID删除用户
    @DeleteMapping("/delete/{id}")
    public Response<String> deleteUserById(@PathVariable Integer id) {
        int result = userMapper.deleteUserById(id);
        if (result > 0) {
            return new Response<>();
        } else {
            return new Response<>();
        }
    }
}